#!/bin/sh
# - mysql_port
# - root_pwd
# - data_dir
# - fs_type

mysql_port=${mysql_port:-3306}
root_pwd=${root_pwd:-123456}
data_dir=${data_dir:-/data}
fs_type=${fs_type:-xfs}


BENCHMARK_ROOT=/home/test
mkdir -p "${BENCHMARK_ROOT}"
program_root="${LKP_SRC}/programs/mysql_performance"


mount_data_dir()
{
    . $LKP_SRC/lib/job.sh
    . $LKP_SRC/lib/fs_ext.sh
    read_env_vars
    destroy_devices
    local dev_num=0
    for dev in $partitions
    do
        if [ "${fs_type}" == "xfs" ];then
            mkfs.xfs -f "${dev}"
        elif [ "${fs_type}"  == "ext4" ];then
            mkfs.ext4 -F "${dev}"
        else
            echo "not support fs_type ${fs_type}!"

        fi
        mkdir -p "${data_dir[${dev_num}]}"
        mount $dev ${data_dir[${dev_num}]}
        dev_num=$(($dev_num + 1))
    done

}


mysql_install()
{
	yum install -y mysql-server net-tools
        command -v netstat > /dev/null
        if [ $? -ne 0 ]; then
                echo "command netstat not install" >&2 && exit 1
        fi
	command -v mysqld >/dev/null
	if [ $? -ne 0 ]; then
		echo "mysql-server not installed" >&2 && exit 1
	else
		echo "mysql-server install success"
	fi

}

prepare()
{
        cd ${program_root}
        \cp "conf/my.cnf-$(arch)" "${conf_file}" 
        sed -i "s#{mysql_port}#${mysql_port}#" "${conf_file}"
        sed -i "s#{data_dir}#${data_dir}#g" "${conf_file}" 
        
        # mkdir data dir
        groupadd mysql
        useradd -g mysql mysql
        mkdir "${data_dir}"
        rm -rf "${data_dir}/mysql" 
        mkdir -p "${data_dir}/mysql"
        mkdir -p "${data_dir}/mysql/data"
        mkdir -p "${data_dir}/mysql/run"
        mkdir -p "${data_dir}/mysql/share"
        mkdir -p "${data_dir}/mysql/log"
        mkdir -p "${data_dir}/mysql/tmp"
        chown -R mysql:mysql /data
        chown -R mysql:mysql /data/mysql
        echo "" > "${data_dir}/mysql/log/mysql.log"
        chown -R mysql:mysql "${data_dir}/mysql/log/mysql.log"

}



modify_mysql_pwd()
{
        socket_file="${data_dir}/mysql/run/mysql.sock"
        echo mysql --connect-expired-password -uroot -p"${default_pwd}" -S "${socket_file}" -e "alter user 'root'@'localhost' identified by '${root_pwd}'"
        mysql --connect-expired-password -uroot -p"${default_pwd}" -S "${socket_file}" -e "alter user 'root'@'localhost' identified by '${root_pwd}'"
        mysql -uroot -p"${root_pwd}" -S "${socket_file}" -e "create user 'root'@'%' identified by '${root_pwd}'"
        mysql -uroot -p"${root_pwd}" -S "${socket_file}" -e "grant all privileges on *.* to 'root'@'%'"
}


wait_check_mysql()
{
     echo "check mysql start"
     time_out=300
     sleep_time=0
     
     while : 
     do
         echo "sleep 3"
         sleep 3
         mysql_flag=$(netstat -tunlp|grep mysql &>/dev/null;echo $?)
         port_flag=$(netstat -tunlp|grep ${mysql_port} &>/dev/null;echo $?)
         if [[ ${mysql_flag} -eq 0 && ${port_flag} -eq 0 ]];then
                echo "start mysql finished"
                break
         fi
         sleep_time=$((sleep_time + 3))
         if [ ${sleep_time} -gt ${time_out} ];then
            echo "start mysql timeout"
            break && exit 1
         fi
     done

}

start_mysql()
{
        
	systemctl stop firewalld
        echo 3 > /proc/sys/vm/drop_caches
        default_pwd=$(mysqld --defaults-file=${conf_file} --initialize 2>&1|grep 'A temporary password is generated'|tail -n 1|awk '{print $NF}')
        mysqld --defaults-file=${conf_file} 2>&1 |tee  ${BENCHMARK_ROOT}/mysql-start.log &
        wait_check_mysql
        modify_mysql_pwd
        
        if [ $? -ne 0 ];then
            echo "start mysql failed!!!" >&2 && exit 1
        else
            echo "start mysql success"
	fi

}

main()
{
	mount_data_dir
        mysql_install
	conf_file="$BENCHMARK_ROOT/my.cnf"
	prepare
        start_mysql
}

main

